package com.rongx.controller;

import com.rongx.anno.LogOperation;
import com.rongx.pojo.Dept;
import com.rongx.pojo.Result;
import com.rongx.service.DeptService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    //    @RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping
    public Result list() {
        log.info("查询部门列表");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    //    @DeleteMapping("/depts")
//    public Result delete(HttpServletRequest request){
//        String idStr = request.getParameter("id");
//        int id = Integer.parseInt(idStr);
//
//        System.out.println("根据ID删除部门: " + id);
//        return Result.success();
//    }
//    @DeleteMapping("/depts")
    public Result delete(Integer id){
        log.info("根据id删除部门, id: {}" , id);
//        System.out.println("根据id删除部门, id=" + id);
        deptService.deleteById(id);
        return Result.success();
    }

    @LogOperation
    @PostMapping
    public Result save(@RequestBody Dept dept){
        log.info("新增部门, dept: {}" , dept);
//        System.out.println("新增部门，dept"+dept);
        deptService.sava(dept);
        return Result.success();
    }
//    @GetMapping("/depts/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询, id: {}" , id);
//        System.out.println("根据ID查询, id=" + id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
//    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
        log.info("修改部门, dept: {}" , dept);
//        System.out.println("修改部门, dept=" + dept);
        deptService.update(dept);
        return Result.success();
    }
}
